#include "Rectangle.h"
#include "DirectX\Buffer.h"
#include "NeroEngine\NPlatform.h"
#include "NeroCore\NMemoryManager.h"

using namespace Graphics;

NRectangle::NRectangle(void)
	:NEntity()
{
	mBuffer = MY_NEW(Memory::HID_Rendering,"Rectangle VB") Graphics::Buffer();
}

NRectangle::~NRectangle(void)
{
}

void NRectangle::Initialize()
{
#ifdef DX_9
	NEntity::Initialize();

	Graphics::D3DDevice* nDevice = GetPlatform()->GetDevice()->As<Graphics::D3DDevice>();
	LPDIRECT3DDEVICE9 device = nDevice->GetDevice();

	mBuffer->Initialize(device, 6);
	
	CUSTOMVERTEX vertices[] =
	{
		{ 150.0f, 62.5f,  0.5f, D3DCOLOR_XRGB(0, 0, 50), },
        { 650.0f, 500.0f, 0.5f, D3DCOLOR_XRGB(0, 0, 50), },
        { 150.0f, 500.0f, 0.5f, D3DCOLOR_XRGB(0, 0, 50), },
        
		{ 150.0f, 62.5f,  0.5f, D3DCOLOR_XRGB(0, 0, 50), },
        { 650.0f, 62.5f,  0.5f, D3DCOLOR_XRGB(0, 0, 50), },
        { 650.0f, 500.0f, 0.5f, D3DCOLOR_XRGB(0, 0, 50), },
	};

	mBuffer->Lock();

	memcpy(mBuffer->GetMemoryPointer(), vertices, sizeof(vertices));

	mBuffer->Unlock();

	mEnabled = true;
#endif
}

void NRectangle::Render()
{
#ifdef DX_9
	HRESULT result;
	if(mEnabled)
	{
		Graphics::D3DDevice* nDevice = GetPlatform()->GetDevice()->As<Graphics::D3DDevice>();
		LPDIRECT3DDEVICE9 device = nDevice->GetDevice();
	
		mBuffer->Render(device);

		result = device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
	
		CHECK(result)
	}
#endif
}